package com.hnucm.c201901020121.Fragment.Message;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hnucm.c201901020121.R;
import com.hnucm.c201901020121.Utils.Bmob.Chat;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;


public class MessageFragment extends Fragment {
    RecyclerView recyclerView;
    List<Chat> chatList = new ArrayList<>();
    MyAdapter myAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = getActivity().findViewById(R.id.recyclerview1);
        myAdapter = new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        getDate();
    }
    private void getDate() {
        String sql="select * from Chat";
        BmobQuery<Chat> query=new BmobQuery<Chat>();
        query.setSQL(sql);
        query.doSQLQuery(new SQLQueryListener<Chat>() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void done(BmobQueryResult<Chat> bmobQueryResult, BmobException e) {
                if(e==null){
                    List<Chat> list=(List<Chat>) bmobQueryResult.getResults();
                    //Log.i("Bmob", list.get(0).getObjectId());
                    chatList.clear();
                    chatList.addAll(list);
                    myAdapter.notifyDataSetChanged();//刷新列表数据
                }
                else {
                    Log.i("Bmob", "查找失败");
                }
            }
        });
    }
    public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_chat, parent, false);
                return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            int type=getItemViewType(position);
           Chat chat=chatList.get(position);
                MyViewHolder holder1=(MyViewHolder)holder;
                holder1.nameTv.setText(chatList.get(position).getName());
                holder1.contentTv.setText(chatList.get(position).getContent());
                holder1.timeTv.setText(chatList.get(position).getTime());
//            holder.imageView.setImageResource(chatList.get(position).getImg());
//            fragment getActivity()
//            acitivity   this    类名.this
                Glide.with(getActivity()).
                        load(chatList.get(position).head)
                        .into(holder1.imageView);
        }

        @Override
        public int getItemCount() {
            return chatList.size();
        }

        @Override
        public int getItemViewType(int position) {
            return chatList.get(position).getType();
        }
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView contentTv;
        TextView timeTv;
        ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.nametextview);
            contentTv = itemView.findViewById(R.id.contenttextview);
            timeTv = itemView.findViewById(R.id.timetextview);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }

}